## MemoryManager

The `MemoryManager` class is responsible for managing user memories, including creating, retrieving, updating, and deleting memories.

### Constructor Parameters

| Parameter | Type | Description | Default |
|-----------|------|-------------|---------|
| `model` | `Optional[Model]` | Model used for memory operations | `None` |
| `system_message` | `Optional[str]` | Custom system prompt for the memory manager | `None` |
| `memory_capture_instructions` | `Optional[str]` | Custom instructions for memory creation and filtering | `None` |
| `additional_instructions` | `Optional[str]` | Additional instructions added to the system message | `None` |
| `db` | `Optional[BaseDb]` | Database for storing memories | `None` |
| `debug_mode` | `bool` | Whether to enable debug logging | `False` |

### Key Methods

#### User Memory Management

| Method | Description | Parameters | Returns |
|--------|-------------|------------|---------|
| `get_user_memories` | Retrieves all memories for a user | `user_id: Optional[str] = None` | `Optional[List[UserMemory]]` |
| `get_user_memory` | Gets a specific memory by ID | `memory_id: str, user_id: Optional[str] = None` | `Optional[UserMemory]` |
| `add_user_memory` | Adds a new memory and returns the memory ID | `memory: UserMemory, user_id: Optional[str] = None` | `Optional[str]` |
| `replace_user_memory` | Updates an existing memory and returns the memory ID | `memory_id: str, memory: UserMemory, user_id: Optional[str] = None` | `Optional[str]` |
| `delete_user_memory` | Deletes a memory by ID | `memory_id: str, user_id: Optional[str] = None` | `None` |
| `clear` | Clears all memories from the database | None | `None` |

#### Memory Creation and Search

| Method | Description | Parameters | Returns |
|--------|-------------|------------|---------|
| `create_user_memories` | Creates memories from text or messages | `message: Optional[str] = None, messages: Optional[List[Message]] = None, agent_id: Optional[str] = None, team_id: Optional[str] = None, user_id: Optional[str] = None` | `str` |
| `acreate_user_memories` | Creates memories from text or messages (Async) | `message: Optional[str] = None, messages: Optional[List[Message]] = None, agent_id: Optional[str] = None, team_id: Optional[str] = None, user_id: Optional[str] = None` | `str` |
| `search_user_memories` | Searches user memories using specified retrieval method | `query: Optional[str] = None, limit: Optional[int] = None, retrieval_method: Optional[Literal["last_n", "first_n", "agentic"]] = None, user_id: Optional[str] = None` | `List[UserMemory]` |

#### Memory Task Management

| Method | Description | Parameters | Returns |
|--------|-------------|------------|---------|
| `update_memory_task` | Updates memories based on a task description | `task: str, user_id: Optional[str] = None` | `str` |
| `aupdate_memory_task` | Updates memories based on a task description (Async) | `task: str, user_id: Optional[str] = None` | `str` |

#### Utility Methods

| Method | Description | Parameters | Returns |
|--------|-------------|------------|---------|
| `initialize` | Initializes the memory manager | `user_id: Optional[str] = None` | `None` |
| `read_from_db` | Reads memories from the database | `user_id: Optional[str] = None` | `Optional[Dict[str, List[UserMemory]]]` |

### Retrieval Methods

The `search_user_memories` method supports the following retrieval methods:

- **`last_n`**: Returns the most recent memories
- **`first_n`**: Returns the oldest memories  
- **`agentic`**: Returns memories most similar to the query using an AI-powered semantic search
